<template>
	<view class="main">
		<view v-if="isAuth" class="login_view" @tap="login">
			微信登陆
		</view>
		<view v-if="!isAuth" class="login_view">
			微信登陆
			<button class="auth_button" open-type="getUserInfo" lang="zh_CN" @getuserinfo="onGetUserInfo"></button>
		</view>

	</view>
</template>

<script>
	import api from '../../common/api.js';
     import request from '../../common/request.js';
	export default {
		data() {
			return {
				isAuth: false,
			}
		},
		onLoad(option) {
			this.checkAuth()
		},
		methods: {
			/**
			 * @param {Object} user保存用户
			 */
			saveUser(user){
				uni.setStorage({
					key:"user",
					data:JSON.stringify(user)
				})
			},
			
			/**
			 * 保存登录状态标志
			 */
			saveLoginFlag(token) {
				uni.setStorage({
					key: "token",
					data: token
				})
			},
			/**
			 * 判断是否授过权
			 */
			checkAuth() {
				let that = this
				wx.getSetting({
					success: (res) => {
						let auth = res.authSetting;
						if (auth['scope.userInfo']) {
							//授权过
							that.isAuth = true
						} else {
							//没授权
							that.isAuth = false
						}
					}
				})
			},
			//授权登录
			onGetUserInfo(e) {
				uni.showLoading({
					title: "登陆中"
				})
				let userInfo = e.detail.userInfo;
				if (userInfo == null) {
					uni.hideLoading()
					uni.showToast({
						icon: "none",
						title: "用户拒绝授权"
					})
					return
				}
				uni.login({
					provider: "weixin",
					success: (res) => {
						uni.request({
							url: this.$baseUrl + api.AUTH_CODE,
							data: {
								code: res.code
							},
							success: (res1) => {
								let openid = res1.data.data
								userInfo.miniAppOpenid = openid
								userInfo.nickname = userInfo.nickName
								uni.request({
									url: this.$baseUrl + api.SAVE_USER,
									data: userInfo,
									method: "POST",
									success: (res2) => {
										uni.hideLoading()
										if (res2.data.code == 2000) {
											let token = res2.data.data
											this.saveLoginFlag(token)
											this.saveUser({name:userInfo.nickname,avatar:userInfo.avatarUrl})
											uni.showToast({
												icon: "none",
												title: "登录成功"
											})
											uni.navigateBack({
												delta:1
											})
										} else {
											uni.showToast({
												icon: "none",
												title: "登录失败"
											})
										}
									}

								})
							}
						})
					}
				})
			},
			//直接登录
			login() {

				uni.showLoading({
					title: "登录中"
				})
				uni.login({
					provider: 'weixin',
					success: (res) => {
						uni.request({
							url: this.$baseUrl + api.AUTH_CODE,
							data: {
								code: res.code
							},
							success: (res2) => {
								if (res2.data.code == 2000) {
									let openid = res2.data.data
									uni.getUserInfo({
										lang: "zh_CN",
										success: (res3) => {
											let param = res3.userInfo
											param.miniAppOpenid = openid
											param.nickname = param.nickName
											uni.request({
												url: this.$baseUrl + api.SAVE_USER,
												data: param,
												method: "POST",
												success: (res4) => {
													uni.hideLoading()
													if (res4.data.code == 2000) {
														let token = res4.data.data
														this.saveLoginFlag(token)
														this.saveUser({name:param.nickname,avatar:param.avatarUrl})
														uni.showToast({
															icon: "none",
															title: "登录成功"
														})
														uni.navigateBack({
															delta:1
														})
													} else {
														uni.showToast({
															icon: "none",
															title: "登录失败"
														})
													}
												}

											})
										}
									})

								} else {
									uni.hideLoading()
									uni.showToast({
										icon: "none",
										title: "登录失败"
									})
								}
							}
						})
					}
				})
			}
		}
	}
</script>

<style>
	.main {
		width: 750upx;
		height: 100vh;
		justify-content: center;
		align-items: center;
	}

	.login_view {
		position: relative;
		width: 400upx;
		height: 100upx;
		background-color: #07C160;
		color: #FFFFFF;
		align-items: center;
		justify-content: center;
		font-size: 40upx;
		font-weight: 700;
		border-radius: 100upx;
	}

	.login_view:active {
		color: #07C160;
		border: 1px solid #07C160;
		background-color: #FFFFFF;
	}

	.auth_button {
		position: absolute;
		top: 0;
		left: 0;
		width: 400upx;
		height: 100upx;
		border-radius: 100upx;
		background-color: rgba(0, 0, 0, 0);
	}
	.auth_button:after{
		border: none;
	}
</style>
